<template>
  <el-row>
    <el-row>
      <navigator></navigator>
    </el-row>
    <el-row>
      <h1>生鲜溯源智能合约使用示例</h1>
      <el-form style="text-align: center">
        <el-form-item>
          <el-row>
            <el-col :span="2">
              合约名称：
            </el-col>
            <el-col :span="6">
              <el-input type="primary" v-model="contract_name"></el-input>
            </el-col>
            <el-col :span="2">
              合约地址：
            </el-col>
            <el-col :span="6">
              <el-input type="primary" v-model="contract_address"></el-input>
            </el-col>
          </el-row>
          <el-row style="margin-top: 20px">
            <el-col :span="2">
              合约abi：
            </el-col>
            <el-col :span="6">
              <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 8}" v-model="contract_abi"></el-input>
            </el-col>
            <el-col :offset="2" :span="2">
              <el-button type="primary" @click="set_contract">设置合约信息</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-row :gutter="20">
        <el-col :span="3">
          操作用户地址：
        </el-col>
        <el-col :span="6">
          <el-input type="primary" v-model="opt_user_address"></el-input>
        </el-col>
      </el-row>
      <el-row>
        <el-form label-position="top">
          <el-form-item>
            <h2>角色管理模块</h2>
            <el-row>
              <el-col :span="2">角色选择:</el-col>
              <el-col :span="2">
                <el-select v-model="role" placeholder="请选择">
                <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
                </el-select>
              </el-col>
              <el-col :offset="1" :span="2">
                用户地址：
              </el-col>
              <el-col :span="4">
                <el-input v-model="role_user_address_set" type="primary"></el-input>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" @click="set_user_role">设置</el-button>
              </el-col>
            </el-row>

            <el-row style="margin-top: 20px">
              <el-col :span="2">
                查看用户地址：
              </el-col>
              <el-col :span="4">
                <el-input v-model="role_user_address_get" type="primary"></el-input>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" @click="get_user_role">获取</el-button>
              </el-col>
              <el-col :span="2">
                {{user_role}}
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <h2>食品管理模块</h2>
            <el-row>
              <el-col :span="2">
                设置食品信息：
              </el-col>
              <el-col :span="2">
                名称:
              </el-col>
              <el-col :span="2">
                <el-input type="primary" v-model="feed_name"></el-input>
              </el-col>
              <el-col :span="2">
                类型:
              </el-col>
              <el-col :span="2">
                <el-input type="primary" v-model="feed_type"></el-input>
              </el-col>
              <el-col :span="2">
                价格:
              </el-col>
              <el-col :span="2">
                <el-input type="primary" v-model="feed_price"></el-input>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" @click="set_feed">确定</el-button>
              </el-col>
            </el-row>
            <el-row style="margin-top: 20px">
              <el-col :span="2">
                查看食品信息：
              </el-col>
              <el-col :span="2">
                食品编号:
              </el-col>
              <el-col :span="4">
                <el-input type="primary" v-model="feed_no"></el-input>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" @click="get_feed">查询</el-button>
              </el-col>
              <el-col :span="6">
                {{feed_detail}}
              </el-col>

            </el-row>
          </el-form-item>
          <el-form-item>
            <h2>订单管理</h2>
            <el-row>
              <el-col :span="2">
                创建订单：
              </el-col>
              <el-col :span="2">
                价格:
              </el-col>
              <el-col :span="2">
                <el-input type="primay" v-model="order_price"></el-input>
              </el-col>
              <el-col :span="2">
                食品类型:
              </el-col>
              <el-col :span="2">
                <el-input type="primay" v-model="order_food_type"></el-input>
              </el-col>
              <el-col :span="2">
                食品编号:
              </el-col>
              <el-col :span="2">
                <el-input type="primay" v-model="order_feed_no"></el-input>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" @click="create_order">确定</el-button>
              </el-col>
            </el-row>
            <el-row style="margin-top: 20px">
              <el-col :span="2">
                查询订单信息:
              </el-col>
              <el-col :span="4">
                <el-input type="primay" v-model="order_no"></el-input>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" @click="get_order">查询</el-button>
              </el-col>
              <el-col :span="4">
                {{order_detail}}
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <h2>流程订单管理</h2>
            <el-row>
              <el-col :span="2">
                创建订单：
              </el-col>
              <el-col :span="1">
                订单类型:
              </el-col>
              <el-col :span="3">
                <el-select v-model="specific_order_type" placeholder="请选择">
                  <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="2">
                订单编号:
              </el-col>
              <el-col :span="2">
                <el-input type="primay" v-model="parent_order_no"></el-input>
              </el-col>
              <el-col :span="2">
                订单价格:
              </el-col>
              <el-col :span="2">
                <el-input type="primay" v-model="specific_order_price"></el-input>
              </el-col>
              <el-col :span="2">
                订单详情:
              </el-col>
              <el-col :span="2">
                <el-input type="primay" v-model="specific_order_detail"></el-input>
              </el-col>
              <el-col :span="2">
                食品编号:
              </el-col>
              <el-col :span="2">
                <el-input type="primay" v-model="specific_order_feed_no"></el-input>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" @click="create_specific_order">确定</el-button>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="2">
                查看订单：
              </el-col>
              <el-col :span="1">
                订单类型:
              </el-col>
              <el-col :span="3">
                <el-select v-model="specific_order_get_type" placeholder="请选择">
                  <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="2">
                订单编号:
              </el-col>
              <el-col :span="4">
                <el-input type="primay" v-model="parent_order_no_get"></el-input>
              </el-col>
              <el-col :span="2">
                <el-button type="primary" @click="get_specific_order">确定</el-button>
              </el-col>
              <el-col :span="6">
                {{specific_order_get_detail}}
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-row>


    </el-row>

  </el-row>


</template>

<script>
import Navigator from "@/components/Navigator";
export default {
  name: "FeedTrace",
  components: {
    Navigator,
    // jsonView
  },
  data() {
    return {
      contract_name:"",
      contract_address:"",
      contract_abi:"",
      opt_user_address:"",
      // 角色管理
      options: [{
        value: '1',
        label: '生产'
      }, {
        value: '2',
        label: '加工'
      }, {
        value: '3',
        label: '运输'
      }, {
        value: '4',
        label: '仓库'
      }, {
        value: '5',
        label: '销售'
      }],
      role: '',
      role_user_address_set:'',
      role_user_address_get:'',
      user_role:'',

      //食品管理
      feed_name:'',
      feed_type:'',
      feed_price:'',

      feed_no:'',
      feed_detail: "",

      //订单管理
      order_price: '',
      order_food_type: '',
      order_feed_no:'',
      order_detail:'',
      order_no:'',

      //流程订单管理
      specific_order_type:'',
      parent_order_no:'',
      specific_order_price: '',
      specific_order_detail: '',
      specific_order_feed_no: '',

      specific_order_order_detail:'',
      specific_order_order_no:'',

      specific_order_get_type:'',
      parent_order_no_get: '',
      specific_order_get_detail:''


    }
  },
  methods: {
    set_contract() {
      let data = {
        name: this.contract_name,
        address: this.contract_address,
        abi: this.contract_abi
      }
      this.axios.post(`contract/set`, data).then((response) => {
        console.log(response);
        if (response.data.code == 200) {
          alert("设置成功")
        }else {
          alert("存在问题")
        }

      })
    },
    set_user_role() {
      let data = {
        user: this.opt_user_address,
        address: this.role_user_address_set,
        role: this.role
      }
      this.axios.post(`contract/feedtrace/set_user_role`, data).then((response) => {
        console.log(response);
        if (response.data.code == 200) {
          alert("设置成功!")
        }else {
          alert("设置失败")
        }
      })
    },
    get_user_role() {
      let data = {
        user: this.opt_user_address,
        address: this.role_user_address_get
      }
      this.axios.post(`contract/feedtrace/get_user_role`,data).then((response) => {
        console.log(response)
        if (response.data.code == 200) {
          if (response.data.data == 1) {
            this.user_role = '生产'
          }else if (response.data.data == 2) {
            this.user_role = '加工'
          }else if (response.data.data == 3) {
            this.user_role = '运输'
          }else if (response.data.data == 4) {
            this.user_role = '仓库'
          }else if (response.data.data == 5) {
            this.user_role = '销售'
          }
        }
      })
    },
    set_feed() {
      let data = {
        user: this.opt_user_address,
        feed_name : this.feed_name,
        feed_type : this.feed_type,
        feed_price: this.feed_price
      }
      this.axios.post(`contract/feedtrace/create_feed`, data).then((response) => {
        console.log(response);
        if (response.data.code == 200) {
          alert("设置成功!")
        }else {
          alert("设置失败")
        }
      })
    },
    get_feed() {
      let data = {
        user: this.opt_user_address,
        feed_no: this.feed_no
      }
      this.axios.post(`contract/feedtrace/get_feed`, data).then((response) => {
        console.log(response);
        if (response.data.code == 200) {
          this.feed_detail = response.data.data
          alert("获取成功!")
        }else {
          alert("设置失败")
        }
      })
    },
    create_order() {
      let data = {
        user: this.opt_user_address,
        order_price: this.order_price,
        food_type: this.order_food_type,
        feed_no: this.order_feed_no
      }
      this.axios.post(`contract/feedtrace/create_order`, data).then((response) => {
        console.log(response);
        if (response.data.code == 200) {
          alert("设置成功!")
        }else {
          alert("设置失败")
        }
      })
    },
    get_order() {
      let data = {
        user: this.opt_user_address,
        order_no : this.order_no
      }
      this.axios.post(`contract/feedtrace/get_order`, data).then((response) => {
        console.log(response);
        if (response.data.code == 200) {
          this.order_detail = response.data.data
          alert("获取成功!")
        }else {
          alert("设置失败")
        }
      })
    },
    create_specific_order() {
      let data = {
        user: this.opt_user_address,
        order_type: this.specific_order_type,
        parent_order_no: this.parent_order_no,
        order_price: this.specific_order_price,
        order_detail: this.specific_order_detail,
        order_feed_no: this.specific_order_feed_no
      }
      this.axios.post(`contract/feedtrace/create_specific_order`, data).then((response) => {
        console.log(response);
        if (response.data.code == 200) {
          alert("设置成功!")
        }else {
          alert("设置失败")
        }
      })
    },
    get_specific_order() {
      let data = {
        user: this.opt_user_address,
        order_type: this.specific_order_get_type,
        parent_order_no: this.parent_order_no_get
      }
      this.axios.post(`contract/feedtrace/get_specific_order`, data).then((response) => {
        console.log(response);
        if (response.data.code == 200) {
          alert("获取成功!")
          this.specific_order_get_detail = response.data.data
        }else {
          alert("获取失败")
        }
      })
    }
  }
}
</script>

<style scoped>

</style>